#!/bin/bash
echo "Building complete AI communication system..."
echo

WXWIN="/d/vc/wxWidgets-3.2.7"

# 设置 PATH
export PATH="$WXWIN/lib:$PATH"

# 创建完整的 AI 主程序
cat > src/complete_ai_system.cpp << 'COMPLETE_EOF'
#include <wx/wx.h>
#include <wx/utils.h>
#include <wx/file.h>
#include <wx/textfile.h>
#include <iostream>
#include <string>
#include <vector>
#include <map>

class AITerminal {
private:
    std::vector<std::string> history;
    
public:
    bool ExecuteCommand(const std::string& command) {
        std::cout << "🖥️  Executing: " << command << std::endl;
        
        // 这里可以调用系统命令
        // 现在只是模拟
        wxString wxCommand(command.c_str(), wxConvUTF8);
        std::cout << "💭 Processing: " << command << std::endl;
        
        history.push_back("Command: " + command);
        return true;
    }
    
    void ShowHistory() {
        std::cout << "📜 Command History:" << std::endl;
        for (const auto& entry : history) {
            std::cout << "  " << entry << std::endl;
        }
    }
};

class HumanAITerminal {
private:
    std::vector<std::string> conversation;
    AITerminal computer;
    
public:
    void StartInteractive() {
        std::cout << "🚀 AI Communication System" << std::endl;
        std::cout << "==========================" << std::endl;
        std::cout << "Human↔AI↔Computer Communication" << std::endl;
        std::cout << "Type 'exit' to quit, 'help' for commands" << std::endl;
        
        char buffer[256];
        while (true) {
            std::cout << "human> ";
            std::flush(std::cout);
            
            if (!std::cin.getline(buffer, sizeof(buffer))) {
                break;
            }
            
            std::string input(buffer);
            if (input == "exit" || input == "quit") {
                std::cout << "Goodbye! 👋" << std::endl;
                break;
            }
            else if (input == "help") {
                ShowHelp();
            }
            else if (input == "status") {
                ShowStatus();
            }
            else if (input == "history") {
                computer.ShowHistory();
            }
            else if (input == "wxinfo") {
                ShowWxInfo();
            }
            else if (input == "files") {
                ListFiles();
            }
            else if (!input.empty()) {
                ProcessHumanInput(input);
            }
        }
    }
    
    void ProcessHumanInput(const std::string& input) {
        conversation.push_back("Human: " + input);
        std::cout << "🤖 AI: Received: " << input << std::endl;
        
        // AI 处理逻辑
        if (input.find("read") != std::string::npos || input.find("open") != std::string::npos) {
            std::cout << "📖 AI: Reading file operation..." << std::endl;
            computer.ExecuteCommand("file_read " + input);
        }
        else if (input.find("write") != std::string::npos || input.find("create") != std::string::npos) {
            std::cout << "📝 AI: Writing file operation..." << std::endl;
            computer.ExecuteCommand("file_write " + input);
        }
        else if (input.find("compile") != std::string::npos || input.find("build") != std::string::npos) {
            std::cout << "🔨 AI: Build operation..." << std::endl;
            computer.ExecuteCommand("build " + input);
        }
        else if (input.find("list") != std::string::npos || input.find("dir") != std::string::npos) {
            std::cout << "📁 AI: Listing files..." << std::endl;
            computer.ExecuteCommand("list_files");
        }
        else {
            std::cout << "💭 AI: Processing command: " << input << std::endl;
            computer.ExecuteCommand(input);
        }
        
        conversation.push_back("AI: Processed command");
    }
    
    void ShowHelp() {
        std::cout << "Available commands:" << std::endl;
        std::cout << "  read <file>     - Read a file" << std::endl;
        std::cout << "  write <file>    - Write to a file" << std::endl;
        std::cout << "  compile <proj>  - Compile a project" << std::endl;
        std::cout << "  list, dir       - List files" << std::endl;
        std::cout << "  status          - Show system status" << std::endl;
        std::cout << "  wxinfo          - Show wxWidgets info" << std::endl;
        std::cout << "  history         - Show command history" << std::endl;
        std::cout << "  files           - List current directory files" << std::endl;
        std::cout << "  help            - Show this help" << std::endl;
        std::cout << "  exit            - Exit program" << std::endl;
    }
    
    void ShowStatus() {
        std::cout << "System Status:" << std::endl;
        std::cout << "  AI System: Running" << std::endl;
        std::cout << "  wxWidgets: " << wxVERSION_STRING << std::endl;
        std::cout << "  Directory: " << (const char*)wxGetCwd().mb_str() << std::endl;
        std::cout << "  Conversation: " << conversation.size() << " messages" << std::endl;
    }
    
    void ShowWxInfo() {
        std::cout << "wxWidgets Information:" << std::endl;
        std::cout << "  Version: " << wxVERSION_STRING << std::endl;
        std::cout << "  Unicode: Enabled" << std::endl;
        std::cout << "  Platform: Windows" << std::endl;
    }
    
    void ListFiles() {
        std::cout << "📁 Current Directory Files:" << std::endl;
        wxString cwd = wxGetCwd();
        wxDir dir(cwd);
        
        if (!dir.IsOpened()) {
            std::cout << "  Cannot open directory" << std::endl;
            return;
        }
        
        wxString filename;
        bool cont = dir.GetFirst(&filename, "*.cpp", wxDIR_FILES);
        while (cont) {
            std::cout << "  " << (const char*)filename.mb_str() << std::endl;
            cont = dir.GetNext(&filename);
        }
    }
};

int main(int argc, char* argv[]) {
    wxInitialize();
    
    HumanAITerminal ai;
    
    if (argc > 1) {
        std::string arg = argv[1];
        if (arg == "--interactive" || arg == "-i") {
            ai.StartInteractive();
        }
        else if (arg == "--status" || arg == "-s") {
            ai.ShowStatus();
        }
        else if (arg == "--help" || arg == "-h") {
            std::cout << "Usage: " << argv[0] << " [options]" << std::endl;
            std::cout << "Options:" << std::endl;
            std::cout << "  --interactive, -i  Start interactive AI terminal" << std::endl;
            std::cout << "  --status, -s       Show system status" << std::endl;
            std::cout << "  --help, -h         Show this help" << std::endl;
            std::cout << "  --version, -v      Show version" << std::endl;
        }
        else if (arg == "--version" || arg == "-v") {
            std::cout << "cbp2make_ai v1.0" << std::endl;
            std::cout << "Complete AI Communication System" << std::endl;
            std::cout << "Built with wxWidgets " << wxVERSION_STRING << std::endl;
        }
    } else {
        ai.StartInteractive();
    }
    
    wxUninitialize();
    return 0;
}
COMPLETE_EOF

# 编译完整版本
echo "Compiling complete AI system..."
g++ -std=c++11 -O2 -Wall -Wextra \
    -I"$WXWIN/include" \
    -D__WXMSW__ -D_UNICODE -DUNICODE \
    src/complete_ai_system.cpp \
    -L"$WXWIN/lib" \
    -lwxmsw32u_core_gcc_custom -lwxbase32u_gcc_custom \
    -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 \
    -ladvapi32 -lshell32 -lole32 -loleaut32 -luuid \
    -lcomctl32 -lrpcrt4 -loleacc -luxtheme \
    -o cbp2make_ai_complete.exe

if [[ $? -eq 0 ]]; then
    echo "✅ Complete AI system compiled successfully!"
    echo "Executable: cbp2make_ai_complete.exe"
    
    echo "Testing..."
    ./cbp2make_ai_complete.exe --status
    
    echo
    echo "🎉 AI Communication System is ready!"
    echo "Run with: ./run_ai_system.sh --interactive"
else
    echo "❌ Compilation failed"
    exit 1
fi
